package divinity.basic
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import divinity.utils.Filters;

	public class Display extends DivinityObject
	{
		private var _sceneContainer:DivinityObject;
		private var _movingContainer:DivinityObject;
		private var _vfx:DivinityObject;
		private var _objects:DivinityObject;
		private var _air:DivinityObject;
		private var _ui:DivinityObject;
		private var _width:int=0;
		private var _height:int=0;
		
		private var _displayBMP:Bitmap
		private var _render:Sprite;
		
		private var _running=true;
		private var _fps=50;
		private var _startTime:int;
		private var _time:Number;
		public function Display(w:int,h:int)
		{
			super();
			_width=w;
			_height=h;
		
			_displayBMP=new Bitmap();
			//addChild(_displayBMP);
			
			_render=new Sprite();
			addChild(_render);
			
			_sceneContainer=new DivinityObject();
			_sceneContainer.name="Whole scene";
			_render.addChild(_sceneContainer);
			
			_ui=new DivinityObject();
			_ui.name="User interface";
			addChild(_ui);
			
			_movingContainer=new DivinityObject();
			_movingContainer.name="main terrain container";
			
			_vfx=new DivinityObject();
			_vfx.name="main vfx container";
			
			
			_objects=new DivinityObject();
			_objects.name="objects container";
		
			_air=new DivinityObject();
			_air.name="Objects that are in the air";
			
			
			/*
			*
			*
			*/			
			_sceneContainer.addChild(_movingContainer);		
			_sceneContainer.addChild(_objects);
			_sceneContainer.addChild(_vfx);
			_sceneContainer.addChild(_air);
			
			
			/*
			*
			*
			*
			*/
			RENDER();
		}
		/*
		*
		*
		*
		*
		*
		*
		*/
		
		override public function init(e:Event)
		{
			_startTime=getTimer();
			_time=0;
			//addEventListener(Event.ENTER_FRAME,cycle);
			super.init(e);
		}
		override public function destroy(e:Event)
		{
			
			removeEventListener(Event.ENTER_FRAME,cycle);
			super.destroy(e);
		}
		private function cycle(e:Event)
		{
			var elapsed=getTimer();
			elapsed=elapsed-_startTime;
		
			_time+=elapsed;
			//trace(_time,1000/_fps);
			if(_time>(1000/_fps))
			{
				trace("display cycle");
				RENDER();
				_time=0;
			}
			
			_startTime=getTimer();
		}
		/*
		*
		*
		*
		*
		*/
		public function centerOn(me:Object)
		{
			var point:Point=new Point(me.x,me.y);
			point=_sceneContainer.localToGlobal(point);
			_sceneContainer.x=(_width/2)-point.x;
			_sceneContainer.y=(_height/2)-point.y;
			
		}
		public function RENDER()
		{
			var bmd:BitmapData=new BitmapData(_width,_height);
			bmd.draw(_render);
			_displayBMP.bitmapData=bmd;
		}
		/*
		*
		*
		*
		*
		*
		*
		*/
		public function get background():DivinityObject
		{
			return _movingContainer;
		}
		public function get objects():DivinityObject
		{
			return _objects;
		}		
		public function get vfx():DivinityObject
		{
			return _vfx;
		}
		public function get air():DivinityObject
		{
			return _air;
		}
		public function get scene():DivinityObject
		{
			return _sceneContainer;	
		}
		public function get ui():DivinityObject
		{
			return _ui;
		}
		
		
		
	}
}